package com.lanling.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanling.educenter.entity.Admin;
import com.lanling.educenter.entity.vo.AdminVo;
import com.lanling.educenter.mapper.AdminMapper;
import com.lanling.educenter.other.ZhenhuaException;
import com.lanling.educenter.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanling.educenter.utils.JwtUtils;
import com.lanling.educenter.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 胡振华
 * @since 2021-02-13
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public Admin login(Admin admin) {
        String username = admin.getUsername();
        String password = admin.getPassword();

        //判断用户名和密码是否为空
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new ZhenhuaException(20001,"登录失败");
        }

        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Admin searchAdmin = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(searchAdmin == null) {
            throw new ZhenhuaException(20001,"登录失败");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(searchAdmin.getPassword())) {
            throw new ZhenhuaException(20001,"登录失败");
        }

        return searchAdmin;
    }

    @Override
    public Admin info(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request.getHeader("admintoken"));
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Admin admin = baseMapper.selectOne(queryWrapper);
        if (admin == null){
            throw new ZhenhuaException(20001,"没有查询到此管理员用户");
        }
        return admin;
    }

    @Override
    public Admin updateProfile(Admin admin) {
        int count = baseMapper.updateById(admin);
        if (count == 0){
            throw new ZhenhuaException(20001,"更新用户信息错误");
        }else{
            return baseMapper.selectById(admin.getId());
        }
    }

    @Override
    public String updateNickname(Admin admin) {
        int i = baseMapper.updateById(admin);
        if (i == 0){
            throw new ZhenhuaException(20001,"更新失败");
        }else{
            String jwtToken = JwtUtils.getJwtToken(admin.getId(), admin.getNickname());
            return jwtToken;
        }
    }

    @Override
    public Admin updatePassword(AdminVo adminVo) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminVo,admin);
        admin.setPassword(MD5.encrypt(adminVo.getNewPassword()));
        int i = baseMapper.updateById(admin);
        if (i == 0){
            throw new ZhenhuaException(20001,"修改密码错误");
        }else{
            return admin;
        }
    }
}
